package com.aruba.mvvmapplication.extension

import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LiveData
import androidx.paging.PagingData
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.RecyclerView

/**
 * Created by aruba on 2021/9/25.
 */
fun <VH : RecyclerView.ViewHolder, T : Any> RecyclerView.setPagingAdapter(
    owner: LifecycleOwner,
    adapter: PagingDataAdapter<T, VH>,
    liveData: LiveData<PagingData<T>>,
    observe: ((data: PagingData<T>) -> Unit)? = null
) {
    liveData.observe(owner) {
        adapter.submitData(owner.lifecycle, it)
        observe?.invoke(it)
    }
}